<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:insert="~{include :: header('订单列表')}" />
    <th:block th:insert="~{include :: bootstrap-table-css}" />
</head>
<body class="hold-transition">
<!-- Site wrapper -->
<div class="container-fluid mb-2">
    <div class="toolbar" id="toolbar" role="group">
        <form id="formId" class="form-inline">
            <input type="text" class="form-control mr-1" placeholder="订单id/订单编号" name="orderNo"/>
            <select name="orderType" class="form-control mr-1" th:with="type=${@dict.getType('qdx_order_type')}">
                <option value="">类型</option>
                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
            </select>
            <select name="status" class="form-control mr-1" th:with="type=${@dict.getType('qdx_order_state')}">
                <option value="">状态</option>
                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
            </select>
            <button type="button" class="btn btn-primary mr-1" onclick="$.table.search()">
                <i class="fa fa-search"></i> 搜索
            </button>
            <button type="button" class="btn btn-warning" onclick="$.form.reset()">
                <i class="fa fa-refresh"></i> 重置
            </button>
            <input type="hidden" name="userId" th:value="${userId}">
        </form>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <table id="bootstrap-table" class="table-striped"></table>
        </div>
    </div>
</div>
<th:block th:insert="~{include :: footer}" />
<th:block th:insert="~{include :: bootstrap-table-js}" />
<script th:inline="javascript">
    var prefix = "/order";
    var editFlag = [[${@permission.hasPermi('order:edit')}]];
    var orderTypeDatas = [[${@dict.getType('qdx_order_type')}]];
    var statusDatas = [[${@dict.getType('qdx_order_state')}]];
    $(function() {
        var options = {
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            url: "/order/list.json",
            modalName: "订单",
            columns: [
                {
                    field: 'orderId',
                    title: '订单id'
                },
                {
                    field: 'orderNo',
                    title: '订单号', formatter: function (value, row, index) {
                        return "<a href='javascript:void(0)' onclick=orderInfo('"+value+"')>"+value+"</a>";
                    }
                },
                {
                    field: 'userName',
                    title: '用户名'
                },
                {
                    field: 'payAmount',
                    title: '金额',
                    formatter: function(value, row, index) {
                        return row.payAmount / 100;
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'orderType',
                    title: '类型',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(orderTypeDatas, value);
                    }
                },
                {
                    field: 'createTime',
                    title: '下单时间'
                },
                {
                    title: '操作',
                    align: 'left',
                    width: '200px',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" onclick=orderInfo(\'' + row.orderNo + '\')><i class="fa fa-search"></i>详情</a> ');
                        var status =row.status;
                        switch (status)
                        {
                            case "INIT":
                                actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick=cancelOrRegain("'+row.orderId+'","CANCEL")><i class="fa fa-remove"></i>取消</a> ');
                                actions.push('<a class="btn btn-primary btn-xs " href="javascript:void(0)" onclick=auditing(\'' + row.orderNo + '\')><i class="fa fa-check"></i>开通</a>');
                                break;
                            case "CANCEL":
                                actions.push('<a class="btn btn-warning btn-xs " href="javascript:void(0)" onclick=cancelOrRegain("'+row.orderId+'","INIT")><i class="fa fa-refresh"></i>恢复</a> ');
                                break;
                            case "SUCCESS":
                                actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick=refund(\'' + row.orderId + '\')><i class="fa fa-database"></i>退款</a> ');
                                break;
                            default:
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //取消或恢复订单
    function cancelOrRegain(oid,state) {
        table.set();
        var str ="";
        if(state=="CANCEL"){
            str = "确定要取消该订单吗？"
        }else{
            str = "确定要恢复该订单吗？"
        }
        $.modal.confirm(str, function() {
            var data = {"orderId":oid,"status": state};
            $.operate.submit(prefix+"/status.json", "post", "json", data);
        });
    }

    //开通订单
    function auditing(number) {
        table.set();
        $.modal.confirm("确定要开通该订单吗？", function() {
            var data = {"orderNo":number};
            $.operate.submit(prefix+"/open.json", "post", "json", data);
        });
    }

    //退款
    function refund(orderId) {
        table.set();
        $.modal.confirm("您确定要退款？退款后用户不能观看此视频！！", function() {
            layer.prompt({title: '请输入退款金额：', formType: 0}, function(pass, index){
                if(isNaN(pass)){
                    $.modal.msgError('退款金额为数字类型！')
                    return false;
                }
                if(pass <= 0){
                    $.modal.msgError('退款金额必选大于0元！')
                    return false;
                }
                layer.close(index);
                layer.prompt({title: '请输入退款备注：', formType: 2}, function(text, index){
                    layer.close(index);
                    $.operate.submit(prefix+"/refund.json", "post", "json", {orderId:orderId,refundAmount:pass,description:text});
                });
            });


        });
    }

    //订单详情页
    function orderInfo(orderNo) {
        var index = layer.open({
            title: '订单详情',
            content: prefix+"/view.html?orderNo="+orderNo,
            type: 2,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            area: ['75%','95%']
        });
    }
</script>
</body>
</html>